{% extends 'base.twig' %}
{% block style %}
    <link href="template/plugins/pnotify/css/pnotify.custom.css" rel="stylesheet" type="text/css">
    <script src="template/plugins/pnotify/js/pnotify.custom.js" type="text/javascript"></script>
{% endblock %}
{% block content %}
    <!--main content start-->
    <link href="template/plugins/dialog/dist/ui-dialog.css" rel="stylesheet">
    <section id="main-content">
        <section class="wrapper">
            <div class="row mt">
                <div class="col-lg-12">
                    <ul id="myTab" class="nav nav-tabs">
                        <li class="active">
                            <a href="#home" data-toggle="tab">
                                地区列表
                            </a>
                        </li>
                        <li>
                            <a href="javascript:void(0);" class="btn btn-default add" data-id="0">
                                <i class="fa fa-plus"></i> 一级地区
                            </a>
                        </li>
                        <li>
                            <a class="add-son btn btn-default" href="javascript:void(0);" ><i class="fa fa-plus">下级区域</i></a>
                        </li>
                    </ul>
                    <div id="myTabContent" class="tab-content">
                        <div class="tab-pane fade in active" id="home">
                            <table class="table table-bordered ">
                                <thead>
                                <tr>
                                    <th>ID</th>
                                    <th>所属国家</th>
                                    <th>区域</th>
                                    <th>地区编码</th>
                                    <th>货币符号</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                {% for g in goodsList %}
                                   <tr>
                                       <td><input type="checkbox" value="{{ g.id_zone }}" name="id_zone[]"> {{ g.id_zone }}</td>
                                       <td>{{ g.country }}</td>
                                       <td>{{ g.title }} ({{ g.lang }})</td>
                                       <td>{{ g.code }}</td>
                                       <td>{{ g.currency }}</td>
                                       <td >
                                           <a class="edit" data-id="{{ g.id_zone }}" href="javascript:void(0);"><i class="fa fa-edit">编辑</i></a>
                                           
                                       </td>
                                   </tr>
                                    {% for s in g.son %}
                                        <tr>
                                            <td><input type="checkbox" value="{{ s.id_zone }}" name="id_zone[]"> {{ s.id_zone }}</td>
                                            <td>{{ g.country }} -- {{ g.lang }}</td>
                                            <td> --| {{ s.title }} ({{ s.lang }})</td>
                                            <td>{{ s.code }}</td>
                                            <td>{{ s.currency }}</td>
                                            <td >
                                                <a class="edit" data-id="{{ s.id_zone }}" href="javascript:void(0);"><i class="fa fa-edit">编辑</i></a>

                                            </td>
                                        </tr>
                                    {% endfor %}
                                {% endfor %}
                                </tbody>
                            </table>
                            <button class="btn btn-danger delete"><i class="fa fa-trash-o"> 删除</i></button>
                            <div class="text-center">
                                {{ pageHtml|raw }}
                            </div>
                        </div>
                    </div>
                </div><!-- /col-lg-4 -->
            </div><!-- /row -->
        </section>
    </section>
    <script src="template/plugins/dialog/dist/dialog-plus-min.js?"></script>
    <script type="text/javascript">
        highlight_subnav('country.php?');
        $('.edit,.add').on('click',function(){
            var obj=$(this);
            var  id = obj.attr('data-id');
            var _url= "country.php?act=edit&id="+id;
            dialog({
                id:'edit',
                title:'编辑',
                url:_url,
                width:400,
                height:600,
                data:obj
            }).showModal();
        });

        $('.add-son').on('click',function(){
            var obj=$(this);
            var _url= "country.php?act=add_son";
            dialog({
                id:'edit',
                title:'增加下级区域',
                url:_url,
                width:1000,
                height:700,
                data:obj
            }).showModal();
        });
        
        function ModalClose(){
            dialog.get('edit').close();
            new PNotify({
                title: '成功！',
                text: '操作成功',
                type: 'success',
                addclass: 'stack-bar-top'
            });
            setTimeout(function () {
                location.reload();
            },2000)
        }

        $(".delete").click(function () {
              var id =[];
              var obj = $("input[name='id_zone[]']:checked");
              console.log(obj.length)
              if(obj.length ==0)
              {
                  new PNotify({
                      title: '失败！',
                      text: '找不到对象！',
                      type: 'error',
                      addclass: 'stack-bar-top'
                  });
                  return;
              }
              obj.each(function () {
                  id.push($(this).val());
              });
            
              if(confirm("是否删除？删除将不可逆转，请谨慎操作")) {
                  $.post('country.php?act=delete',{id:id}).success(function (data) {
                    var ret = JSON.parse(data);
                    if(!ret.ret)
                    {
                        new PNotify({
                            title: '失败！',
                            text: ret.msg,
                            type: 'error',
                            addclass: 'stack-bar-top'
                        });
                        return;
                    }
                    new PNotify({
                          title: '成功！',
                          text: '操作成功',
                          type: 'success',
                          addclass: 'stack-bar-top'
                      });
                      setTimeout(function () {
                          location.reload();
                      },2000)
                  })
              }
        })
       
    </script>
{% endblock %}